﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using LinqToTwitter;
using Microsoft.Phone.Shell;

namespace MolfMolfMolf
{
    public partial class Comment : PhoneApplicationPage
    {
        private static string hashTag = String.Empty;
        private ITwitterAuthorizer auth;
        private string tweetMessage = "#{0} {1} It had my {2}/5 rate via @MolfApp";

        public Comment()
        {
            InitializeComponent();
            initAppBar();

            Loaded += new RoutedEventHandler(Comment_Loaded);
        }

        private void initAppBar()
        {
            ApplicationBar = new ApplicationBar();
            ApplicationBarIconButton SubmitButton = new ApplicationBarIconButton(new Uri("/Images/appbar.upload.rest.png", UriKind.Relative));
            SubmitButton.Text = "submit";
            SubmitButton.Click += new EventHandler(SubmitButton_Click);
            ApplicationBar.Buttons.Add(SubmitButton);
            ApplicationBar.IsVisible = true;
        }

        void SubmitButton_Click(object sender, EventArgs e)
        {
            //send
            var twitterCtx = new TwitterContext(auth);

            twitterCtx.UpdateStatus(String.Format(tweetMessage, hashTag, CommentBox.Message, starRating1.rate),
                updateResp => Dispatcher.BeginInvoke(() =>
                {
                    switch (updateResp.Status)
                    {
                        case TwitterErrorStatus.Success:
                            Status tweet = updateResp.State;
                            User user = tweet.User;
                            UserIdentifier id = user.Identifier;
                            MessageBox.Show("Successfully Posted!");
                            break;
                        case TwitterErrorStatus.TwitterApiError:
                        case TwitterErrorStatus.RequestProcessingException:
                            MessageBox.Show("Fail to post it!");
                            break;
                    }

                    NavigationService.GoBack();
                }));
        }

        void Comment_Loaded(object sender, RoutedEventArgs e)
        {
            if (((App)App.Current).Auth == null || !((App)App.Current).Auth.IsAuthorized)
            {
                NavigationService.Navigate(new Uri("/OAuth.xaml", UriKind.Relative));
            }
            else
            {
                auth = ((App)App.Current).Auth;
            }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            if (NavigationContext.QueryString.ContainsKey("hashtag"))
            {
                hashTag = NavigationContext.QueryString["hashtag"];
            }

            if (((App)App.Current).Auth == null || !((App)App.Current).Auth.IsAuthorized)
            {
                if (e.NavigationMode == System.Windows.Navigation.NavigationMode.Back)
                {
                    NavigationService.GoBack();
                }
            }
        }
    }
}